Ext.define('Zm.controller.DevTree', {
    extend: 'Ext.app.Controller',
    requires:['Zm.Common','Zm.i18n.Lang'],
	stores:['DevTree'],
	refs:[
        {ref:'CardView', selector:'cardview'},
        {ref:'X3dProp', selector:'x3dproperty'},
        {ref:'X3dView', selector:'x3dview'},
        {ref:'X3dPanel',selector:'x3dcontainer'}
	],
    
    init: function(){
        var me = this;
        me.control({
			'devtree':{
                devadd:me.onAdd,
                devdel:me.onDel,
                devrefresh:me.onRef,
                devren:me.onRen,
                selectionchange:me.onSelChg,
                itemdblclick:me.onNodeLocal
			},
            'selectdev button[action=save]':{
                click:me.doAddDev
            }
        });
    },
    doAddDev:function(btn){
        var me = this,
            win = btn.up('window'),
            node = win.actNode,
            frm = win.down('form'),
            form = frm.getForm();
        btn.setDisabled(true);//禁止重复保存
        form.submit({
            url: g_app + '/Dev/add',
            scope: me,
            params:{parentid:node.get('id')},
            success: function(form,action){
                var rd = Ext.create('Zm.model.IdText'),
                    data = action.result.data;
                //在设备树列表中添加新设备,不递归调用了，因为树列表不显示属性子节点
                rd.set('id',data.devid);
                rd.set('text',data.devname);
                node.appendChild(rd);
                rd.commit();
                node.commit();
                //在X3dView中添加新设备
                me.getX3dView().addDev(data);
                win.close();
            },
            failure: Zm.Common.onFail
        });
    },
    onAdd:function(view,node){
        var view = Ext.widget('selectdev');
        view.actNode = node;
    },
    onDel:function(view,node){
        var me = this,view,
            lang = Zm.i18n.Lang;
        if(node){
            Ext.MessageBox.show({
                   title:lang.tDel,
                   msg: Ext.String.format(lang.tDelDevTip,node.get('text')),
                   buttons: Ext.Msg.YESNO,
                   fn: me.onDelCallback,
                   scope: me,
                   node:node,
                   icon: Ext.Msg.QUESTION
            });                                        
        }
    },
    onDelCallback: function(btn,text,opt){
        var me = this,
            lang = Zm.i18n.Lang,
            node = opt.node,
            devid = node.get('id');
            cm = Zm.Common;
        if(btn == 'yes'){
            Ext.Ajax.request({
                url: g_app + '/Dev/del',
                params: {devid: devid},
                scope: me,
                success:function(response,options){ 
                    //移除设备树列表
                    node.remove();
                    //移除X3dView中的设备
                    me.getX3dView().delDev(devid)
                },
                failure:cm.onAFail
             });    
        }
    },
    onRef:function(){
        var me = this;
        me.getDevTreeStore().load();
        me.getX3dView().getLoader().load();
    },
    onRen:function(view,node){
        var me = this,
            lang = Zm.i18n.Lang;
        if(node) Zm.Common.promptMsg(lang.tRename,lang.tDevRenTip,me.doRename,me,false,node.get('text'),node);
    },
    doRename:function(btnId,text,opt){
        var me = this,
            node = opt.opt;
        if(btnId=='ok'){
            Ext.Ajax.request({
                url: g_app + '/Dev/renName',
                params: {devid: node.get('id'),devname:text},
                scope: me,
                success:function(response,options){ 
                     node.set('text',text);
                     node.commit();
                },
                failure:Zm.Common.onAFail
             });            
        }
    },
    onSelChg:function(view,selected,eOpts){
        var me = this,
            node,
            proPanel,
            view = me.getX3dPanel();
        if(selected.length>0){
            me.getCardView().getLayout().setActiveItem(view);
            proPanel = me.getX3dProp();
            node = selected[0];
            //proPanel.setTitle(node.get('text'));
            proPanel.setValue(node.get('id'));
        }
    },
    onNodeLocal:function(view,record){
        var me = this,
            devid = record.get('id');
        me.getX3dView().goDev(devid);
    }
});